package com.sky.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.pojo.Category;
import com.sky.service.admin.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
/**
 * 分类管理
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<Category> getByPage(String name, Integer page, Integer pageSize, Integer type) {
        PageHelper.startPage(page, pageSize);
        return categoryMapper.getByPage(name, type);
    }

    @Override
    public Integer update(Category category) {
        //设置修改时间和修改人
        //category.setUpdateTime(LocalDateTime.now());
        //category.setUpdateUser(BaseContext.getCurrentId());
        //使用切面对字段的填充
        //清理redis缓存
        Category categoryOld = categoryMapper.selectById(category.getId());
        redisTemplate.delete(RedisConstant.CATEGORY_LIST+categoryOld.getType());
        return categoryMapper.update(category);
    }

    @Override
    public Integer save(CategoryDTO categoryDTO) {
        Category category = new Category();
        //属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        //删除缓存
        Category categoryOld = categoryMapper.selectById(category.getId());
        redisTemplate.delete(RedisConstant.CATEGORY_LIST+categoryOld.getType());
         return categoryMapper.save(category);
    }

    @Override
    public Integer delete(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //删除redis缓存
        Category category = categoryMapper.selectById(id);
        redisTemplate.delete(RedisConstant.CATEGORY_LIST+category.getType());
         return categoryMapper.delete(id);

    }
        //查询分类
    @Override
    public List<Category> getByType(Integer type) {
        if (type==null)return categoryMapper.getByType(type);
//  添加redis缓存
        List<Category> list =  (List<Category>)redisTemplate.opsForValue().get(RedisConstant.CATEGORY_LIST+type);
        if(list==null|| list.isEmpty()) {
            list = categoryMapper.getByType(type);

            redisTemplate.opsForValue().set(RedisConstant.CATEGORY_LIST+type,list);
        }
        log.info("list:{}",list);
        return list;
    }
}
